<?php
class PreparationREQ
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqSelectByName;
    private $ReqSelectAll;
    private $ReqInsert;
    private $ReqTruncate;


    public function __construct($db)
    {
    	//variables renseign�es lors de la pr�paration des requ�tes
	
        $this->ReqSelectAll = $db->prepare("SELECT (*) FROM ecole ORDER");
		
        $this->ReqSelectById = $db->prepare("SELECT id, nom FROM ecole WHERE id = ?");
       
 	    $this->ReqSelectByName = $db->prepare("SELECT id, nom FROM ecole WHERE nom LIKE ? ORDER BY nom, id");
        
		$this->ReqInsert = $db->prepare("INSERT INTO ecole(nom, adresse, cp, numtel, mail) VALUES (:nom, :adresse, :cp, :numtel, :mail)");
		
       $this->ReqTruncate = $db->prepare("TRUNCATE ecole");
	   
    }

//M�thodes publiques qui ex�cuteront les requ�tes d�j� pr�par�es dans le constructeur.
	public function insert($nom_ecole, $adresse, $cp, $num_tel, $mail)
    {
        $this->ReqInsert->execute(
            array(':nom' => $nom_ecole, ':adresse' => $adresse, ':cp' => $cp, ':numtel' => $num_tel, ':mail' => $mail));

        return $this->ReqInsert->rowCount();
    }

    public function selectAll()
    {
    	$this->ReqSelectAll->execute();
        return $this->ReqSelectAll->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById ->fetch();
    }

    public function selectByName($nom)
    {
        $this->ReqSelectByName->execute(array('%'.$nom.'%'));
        return $this->ReqSelectByName->fetchAll();
    }
    
	public function truncate()
    {
        return $this->ReqTruncate->execute();
    }
	
}